<?php

namespace api\controllers;

use common\core\actions\GetAction;
use common\core\actions\OptionsAction;
use common\core\actions\PostAction;
use common\core\controller\PublicController;
use common\core\validators\MobileValidator;
use common\helpers\DictHelper;
use server\EquipmentInterface;
use server\UserInterface;
use yii\base\Module;

class LoginController extends PublicController
{

    protected $user;
    protected $lane_devices;

    public function __construct($id, Module $module, UserInterface $user,EquipmentInterface $lane_devices, array $config = [])
    {
        $this->user = $user;
        $this->lane_devices = $lane_devices;
        parent::__construct($id, $module, $config);
    }


    public function actions()
    {
        return [
            'index' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'username',
                    'password_hash',
                ],
                'data' => function ($data) {
                    $data['app_id'] = \Yii::$app->id;
                    return $this->user->login($data, true);
                },
            ],
            'check' => [
                'class' => OptionsAction::class,
            ],
            'refresh' => [
                'class' => GetAction::class,
                'data' => function ($data) {
                    return $this->user->refreshToken($data['token']);
                },
            ],
            'register' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'username',
                    'code',
                    'password_hash',
                ],
                'data' => function ($data) {
                    $user = $this->user->register($data, true);
                    return $this->user->makeToken($user, \Yii::$app->id);
                },
            ],
            'forget-password' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'username',
                    'password',
                    'passwordAgain',
                    'code',
                ],
                'data' => function ($data) {
                    //$this->validateCode($data, 2);
                    if ($data['password'] !== $data['passwordAgain']) {
                        $this->invalidParamException('两次密码不一致');
                    }
                    $this->user->forgetPassword($data);
                    return [];
                },
            ],
            'test_post' => [
                'class' => PostAction::class,
                'data' => function ($data) {
                    file_put_contents('./log.txt', date('Y-m-d H:i:s') . '==>' . json_encode($_POST) . PHP_EOL, FILE_APPEND);
                },
            ],
            'test_get' => [
                'class' => GetAction::class,
                'data' => function () {
                    return $_GET;
                    $s = '2017-02-05 10:10:10';
                    $s = 1517915262;
                    $e = '2018-07-20 12:12:12';
                    $e = 1598700000;
                    return DictHelper::getBetweenMonth($s,$e);
                    //return \Yii::$app->authManager->getRoles();
                },
            ],
            'lane_devices' => [
                'class' => GetAction::class,
                'data' => function () {
                    return $this->lane_devices->getLaneDevices();
                },
            ],

            'rbac' => [
                'class' => GetAction::class,
                'data' => function ($data) {
                    $a = \Yii::$app->authManager->getRoles();
                    $b = \Yii::$app->authManager->getPermissions();
                    $c = \Yii::$app->authManager->getPermissionsByRole('超级管理员');
                    $d = \Yii::$app->authManager->getRules();
                    $e = \Yii::$app->authManager->getPermission('超级管理员权限');
                    //  $b =  \Yii::$app->authManager->getPermissions();
                    return [
                        'a' => $a,
                        //'b'=>$b,
                        'c' => $c,
                        'd' => $d,
                        'e' => $e,
                    ];
                },
            ],
        ];
    }
}
